//
//  ST_PersonalDailyViewController.swift
//  btvideo
//
//  Created by mac on 2020/7/24.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import MJRefresh

class ST_PersonalDailyViewController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
         
        
        
        if let str  = self.title{
            title_nav_btn.setTitleForAllStates(str)
        }
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 44+statusBarHeight, left: 0, bottom: 0, right: 0))
        }
        
        
        tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[weak self] in
            self?.currentPage = 0
            self?.request_data()
        })
        
        request_data()
        
    }
    
//    MARK: - Properties
    
    fileprivate lazy var tableView:UITableView = {
        
        let control = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        
        control.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: BTSafeAreaBottom + 10 + 49 , right: 0)
        
        control.delegate = self
        control.dataSource = self
        
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        
        
        control.register(cellWithClass: ST_DailyMomentCell.self)
        
        control.separatorStyle = .none
        
        control.backgroundColor = .white
        
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        
        return control
    }()
    
//    MARK: - Data
    var userId = ""
    
    fileprivate var currentPage = 0
    
    fileprivate var data_array = [DailyMomentOfHallModel]()
   
    fileprivate func request_data(){
        
        currentPage += 1
        
        let param = ["page":currentPage,
                     "target_accid":userId,
                     "size":20] as [String : Any]
        
        JVrequest(PH.fetchMyDailyMoreListApi,parameters: param, success: { (json) in
            
            print("这个人的特质 数据呢：",json)

            let list = json["data"].arrayValue.map { (itemJson) -> DailyMomentOfHallModel in
                return DailyMomentOfHallModel.init(fromJson: itemJson)
            }
            
            if self.currentPage == 1{
                self.data_array = list
            }else{
                self.data_array += list
            }
            
            
            
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            
            self.tableView.reloadData {
                self.load_more_option_style(tableView: self.tableView, count: list.count, pageSize: 20) {
                    
                    self.tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {[weak self] in

                        self?.request_data()
                    })
                }
            }
            
            
        }) { (code, err) in
            print("看看 数据呢-error：",err)
            self.toast(text: err)
        }
        
    }
    
}


extension ST_PersonalDailyViewController:UITableViewDelegate,UITableViewDataSource{
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data_array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withClass: ST_DailyMomentCell.self, for: indexPath)
        
        cell.model = data_array[indexPath.row]
        
        cell.reload_cell_closure = {[weak self] in
            
            if #available(iOS 13.0, *) {
                self?.tableView.reloadData()
            } else {
                self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.fade)
            }
        }
        
        cell.comment_click_closure = {[weak self] iCell in
            
            guard let model = iCell.model else{ return }
            
            let vc = ST_DaliyMomentDetailController()
            
            vc.dailyId = model.id.string
            
            vc.userId = model.accid
            
            vc.openInputCommentBar = true
            
            self?.navigationController?.pushViewController(vc)
        }
        
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return data_array[indexPath.row].cellHeight
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let model = data_array[indexPath.row]
        
        let vc = ST_DaliyMomentDetailController()
        
        vc.dailyId = model.id.string
        
        vc.userId = model.accid
        
        self.navigationController?.pushViewController(vc)
        
    }
    
    
    
}
